package cn.dakaqi.services.impl;

import cn.dakaqi.dao.GroupServiceDomainDao;
import cn.dakaqi.entities.GroupServiceDomain;
import cn.dakaqi.services.GroupServiceDomainService;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/7/12.
 */
@Component(value = "groupServiceDomain")
@Transactional
public class GroupServiceDomainServiceImpl implements GroupServiceDomainService
{
    @Autowired
    GroupServiceDomainDao groupServiceDomainDao;

    @Override
    public void save(GroupServiceDomain domain) throws ServiceRuntimeException
    {
        groupServiceDomainDao.save(domain);
    }

    @Override
    public List<GroupServiceDomain> queryAllByClient(String client) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.ASC,"code");
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("client", new SearchFilter("client", SearchFilter.Operator.EQ, client));
        filters.put("delStatus", new SearchFilter("delStatus", SearchFilter.Operator.EQ, DKQConstant.DEL_NO));
        Specification<GroupServiceDomain> spec = DynamicSpecifications.bySearchFilter(filters.values(), GroupServiceDomain.class);
        return this.groupServiceDomainDao.findAll(spec,sort);
    }
    @Override
    public void delete(Long id) throws ServiceRuntimeException
    {
        GroupServiceDomain domain = findOne(id);
        if (domain != null)
        {
            domain.setDelStatus(DKQConstant.DEL_YES);
            this.save(domain);
        }
    }

    @Override
    public GroupServiceDomain findOne(long id) throws ServiceRuntimeException
    {
        return groupServiceDomainDao.findOne(id);
    }
}
